﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Entity;
using System.Data;

namespace Core.Dao
{
    class CityDao
    {
        public bool insert(City city)
        {
            object[,] param = {
                {"@Name", city.Name},
                {"@Desc", city.Desc},
                {"@countryId", city.CountryId}
            };
            String sql = "insert into CITIES(CTY_NAME, CTY_DESC, CNT_ID) " +
                "values(@Name, @Desc, @countryId)";
            return DB.exec(sql, param);
        }

        public bool update(City city)
        {
            object[,] param = {
                {"@Name", city.Name},
                {"@Desc", city.Desc},
                {"@id", city.Id},
                {"@countryId", city.CountryId}
            };
            String sql = "update CITIES set CTY_NAME = @Name, CTY_DESC = @Desc, CNT_ID = @countryId " +
                "where CTY_ID = @id";
            return DB.exec(sql, param);
        }

        public bool delete(int id)
        {
            object[,] param = {
                {"@id", id}   
            };
            String sql = "delete from CITIES where CTY_ID = @id";
            return DB.exec(sql, param);
        }

        public bool delete(String ids)
        {
            object[,] param = {
                {"@ids", ids}   
            };
            String sql = "delete from CITIES where CTY_ID in (" + ids + ")";
            return DB.exec(sql);
        }

        public DataTable get(int id)
        {
            object[,] param = {
                {"@id", id}   
            };
            String sql = "select * from CITIES where CTY_ID = @id";
            return DB.getData(sql, param);
        }

        public DataTable getAll(int countryId)
        {
            object[,] param = {
                {"@countryId", countryId}   
            };
            String sql = "select * from CITIES where CNT_ID = @countryId order by CTY_NAME";
            return DB.getData(sql, param);
        }
    }
}
